<?php
namespace Manage\Model;
use Think\Model;
class ArticleModel extends Model{
	private $DescriptionModel;
	private $ImageModel;
	function _initialize(){
		parent::_initialize();
		$this->DescriptionModel=M('ArticleDescription');
		$this->ImageModel=M('ArticleImage');
	}
	public function operArticle($data,$article_id=0){
		$article_data=$this->data($data)->create();
		$article_data['date_modified']=time();
		if($article_id){		
			$this->where(array('article_id'=>$article_id))->save($article_data);
		}else{
			$article_data['date_added']=time();
			$article_id=$this->add($article_data);
		}
		$map['article_id']=$article_id;
		$this->DescriptionModel->where($map)->delete();
		foreach ($data['article_description'] as $language_id => $article_description) {
			$article_description['language_id']=$language_id;
			$article_description['article_id']=$article_id;
			$this->DescriptionModel->data($article_description)->add();
		}
		//图片
		$this->ImageModel->where($map)->delete();
		foreach ($data['article_image'] as $v) {
			$v['article_id']=$article_id;
			$v['text']=json_encode($v['text']);
			$this->ImageModel->add($v);
		}
		//相关
		$ArticleRelated=M('ArticleRelated');
		$ArticleRelated->where($map)->delete();
		$ArticleRelated->where(array('related_id'=>$article_id))->delete();
		foreach ($data['article_related'] as $related_id) {
			$related=array(
				'article_id'	=>	$article_id,
				'related_id'	=>	$related_id
				);
			$ArticleRelated->add($related);
			$related=array('article_id'=>$related_id,'related_id'=>$article_id);
			$ArticleRelated->add($related);
		}
		//附件
		$ArticleToDownload=M('ArticleToDownload');
		$ArticleToDownload->where($map)->delete();
		foreach($data['article_download'] as $download_id){
			$download=array(
				'article_id'=>(int)$article_id,
				'download_id'=>(int)$download_id
				);
			$ArticleToDownload->add($download);
		}
	}
	public function getArticle($article_id){
		$map=array('article_id'=>(int)$article_id);
		$article=$this->where($map)->find();
		$pd=$this->DescriptionModel->where($map)->select();
		foreach ($pd as $v) {
			$article['article_description'][$v['language_id']]=$v;
		}
		$article_image=$this->ImageModel->where($map)->select();
		if($article_image) $article['article_image']=$article_image;
		$article_related=M('ArticleRelated')->where($map)->select();
		foreach ($article_related as $v) {
			$article['article_related'][]=$v['related_id'];
		}
		return $article;
	}
	public function getArticles($data){
		$page=array(
			'start'		=>	$data['start']?$data['start']:0,
			'limit'		=>	$data['limit']?$data['limit']:20
			);
		$map=array(
			'language_id'	=>	get_default_lng_id()
			);
		$order='sort_order ' . (isset($data['order'])?$data['order']:'ASC');
		$order .= ' , article_id DESC ';
		
		if(isset($data['status'])) $map['status']=$data['status'];
		if(isset($data['article_category_id'])) {
			$ids=D('ArticleCategory')->getChildId($data['article_category_id']);
			$map['article_category_id']=array('in',$ids);
		}
		if(isset($data['title'])) $map['title'] = array('like',"%".$data['title']."%");

		$articles=$this->join('LEFT JOIN  __ARTICLE_DESCRIPTION__ ON __ARTICLE__.article_id=__ARTICLE_DESCRIPTION__.article_id')->where($map)->field(C('DB_PREFIX')."article.article_id,title,meta_description,article_category_id,image,status")->limit($page['start'],$page['limit'])->order($order)->select();
		return $articles;
	}
	public function getCount($data){
		$map=array(
			'language_id'	=>	get_default_lng_id()
			);
		if(isset($data['status'])) $map['status']=$data['status'];
		if(isset($data['article_category_id'])) {
			$ids=D('ArticleCategory')->getChildId($data['article_category_id']);
			$map['article_category_id']=array('in',$ids);
		}
		if(isset($data['title'])) $map['title'] = array('like',"%".$data['title']."%");
		$count=$this->join('LEFT JOIN  __ARTICLE_DESCRIPTION__ ON __ARTICLE__.article_id=__ARTICLE_DESCRIPTION__.article_id')->where($map)->count();
		return $count;
	}
	public function del($article_id){
		$map['article_id']=(int)$article_id;
		$this->DescriptionModel->where($map)->delete();
		//图片
		$this->ImageModel->where($map)->delete();
		//关联
		$where_related=array(
			'article_id'	=>	(int)$article_id,
			'related_id'	=>	(int)$article_id,
			'_logic'		=>	'OR'
			);
		M('ArticleRelated')->where($where_related)->delete();
		//附件
		M('ArticleToDownload')->where($map)->delete();
		return $this->where($map)->delete();
	}
}